/*
 * Copyright (c) 2016 4PX Information Technology Co.,Ltd. All rights reserved.
 */
package com.lpz.wdbc;

import java.io.Serializable;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.persistence.Column;
import javax.persistence.Transient;

import com.lpz.wdbc.util.StringUtils;

/**
 * 
 * @author Administrator
 * @date 2017年5月5日
 */
public class EntitySqlHelper implements Serializable{
    
    /**
     * 
     */
    private static final long serialVersionUID = 5111014062412186850L;
    
    
    Class<?> clz = null;
    
    @Transient
    private String tableName;
    
    
    private Map<Field,String> propColumnMap = new HashMap<>();
    
    
    
    private Map<Field,Method> getterMap = new HashMap<>();
    
    
    private Map<Field,Method> setterMap = new HashMap<>();
    
    
    List<Field> dbFields = new ArrayList<>();
    
    EntitySqlHelper(Class<?> clazz){
        
        this.clz = clazz;
        try{
            
            Field[] fields=clazz.getDeclaredFields();
            for (int i = 0; i < fields.length; i++) {
                String column = "";   
                Field f = fields[i];
                
                //静态属性的不要
                int  modifiers  = f.getModifiers();
                if(Modifier.isStatic(modifiers)) continue; 
                
                //如果是Transient属性直接跳过
                Transient t = f.getAnnotation(Transient.class);
                if(t != null) continue;
                
                
                Column c =  f.getAnnotation(Column.class);
                if(c != null) column = c.name();
                else column = StringUtils.camelToUnderline(f.getName());
                
                Class<?> paramClass = f.getType();
                String setterName = StringUtils.getSetterName(f.getName());
                Method setter = clazz.getMethod(setterName, paramClass);
                if(setter !=null) setterMap.put(f, setter);
                
                String getterName = StringUtils.getGetterName(f.getName());
                Method getter = clazz.getMethod(getterName, paramClass);
                if(setter !=null) setterMap.put(f, getter);
                
                
                propColumnMap.put(f, column);
                dbFields.add(f);
            }
        }catch(Exception e){
            e.printStackTrace();
        }
    }
    
    
}
